<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="fmt" uri="jakarta.tags.fmt" %>

<style>
    /* 整体页面背景设置为浅灰色系，营造简洁科技感 */
    body {
        font-family: Arial, sans-serif;
        background-color: #f0f0f5;
        margin: 0;
        padding: 0;
        color: #333;
    }

    /* 页面主容器，设置宽度、居中、内边距、背景色及轻微阴影效果 */
    .main-container {
        width: 60%;
        margin: 30px auto;
        padding: 20px;
        background-color: white;
        box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        border-radius: 8px;
    }

    /* 标题样式，字体大小、加粗、颜色及下边距 */
    h1 {
        font-size: 24px;
        font-weight: bold;
        color: #007bff;
        margin-bottom: 15px;
    }

    /* 表单样式 */
    form {
        padding: 20px;
        border: 1px solid #ccc;
        border-radius: 5px;
        background-color: #f9f9f9;
    }

    /* 表单标签样式 */
    label {
        display: block;
        margin-bottom: 8px;
        font-weight: bold;
        color: #333;
    }

    /* 表单输入框样式 */
    input[type="text"] {
        width: 100%;
        padding: 10px;
        margin-bottom: 15px;
        border: 1px solid #ccc;
        border-radius: 3px;
        box-sizing: border-box;
        transition: border-color 0.3s ease;
    }

    input[type="text"]:focus {
        border-color: #007bff;
    }

    /* 下拉菜单样式 */
    select {
        width: 100%;
        padding: 10px;
        margin-bottom: 15px;
        border: 1px solid #ccc;
        border-radius: 3px;
        box-sizing: border-box;
        appearance: none;
        -webkit-appearance: none;
        -moz-appearance: none;
        background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%23333"><path d="M7 10l5 5 5-5z"/></svg>') no-repeat right 10px center;
        background-size: 12px 12px;
    }

    /* 提交按钮样式 */
    input[type="submit"],
    .back-button {
        display: inline-block;
        text-decoration: none;
        padding: 10px 20px;
        border-radius: 3px;
        font-size: 16px;
        color: white;
        cursor: pointer;
        transition: background-color 0.3s ease, box-shadow 0.3s ease;
        margin-bottom: 10px;
    }

    input[type="submit"] {
        background-color: #28a745;
        box-shadow: 0 3px 6px rgba(0, 0, 0, 0.2);
    }

    input[type="submit"]:hover {
        opacity: 0.8;
        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
    }

    .back-button {
        background-color: #6c757d;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    }

    .back-button:hover {
        opacity: 0.8;
        box-shadow: 0 3px 6px rgba(0, 0, 0, 0.2);
    }

    /* 错误提示信息样式 */
    .error-message {
        color: #dc3545;
        margin-bottom: 10px;
        font-size: 14px;
        display: none;
    }

    /* 编号已存在提示信息样式 */
    .id-exists-message {
        color: #dc3545;
        margin-bottom: 10px;
        font-size: 14px;
        display: none;
    }

    /* 表单元素分组样式，增加元素之间的间距 */
    .form-group {
        margin-bottom: 30px;
    }

    /* 按钮容器样式，将按钮水平排列并添加间距 */
    .button-container {
        display: flex;
        justify-content: flex-end;
        gap: 20px;
        margin-top: 20px;
    }
</style>

<div class="main-container">
    <h1>添加商品类别</h1>
    <form action="category.action?action=save" method="post" id="addForm" >
        <div class="form-group">
            <label for="id">商品类别ID</label>
            <input type="text" id="id" name="id" required>
            <span class="error-message" id="idError">商品类别ID不能为空，且应为正整数</span>
            <span class="id-exists-message" id="idExistsError">该商品类别ID已存在，请重新输入</span>
        </div>

        <div class="form-group">
            <label for="name">商品类别名称</label>
            <input type="text" id="name" name="name" required>
            <span class="error-message" id="nameError">商品类别名称不能为空</span>
        </div>

        <div class="button-container">
            <input type="submit" value="提交">
            <a href="supplier.action?action=list" class="back-button">返回</a>
        </div>
    </form>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
    <script>
        $(document).ready(function () {
            // 商品类别ID输入时，检查是否已存在
            $('#id').on('input', function () {
                var id = $(this).val();
                var xhr = new XMLHttpRequest();
                xhr.onreadystatechange = function () {
                    if (xhr.readyState === 4 && xhr.status === 200) {
                        var exists = JSON.parse(xhr.responseText);
                        if (exists) {
                            $('#idExistsError').show();
                        } else {
                            $('#idExistsError').hide();
                        }
                    }
                };
                xhr.open('GET', 'checkCategoryIdExists.action?id=' + id, true);
                xhr.send();
            });

            // 表单提交时进行验证
            $('#addForm').submit(function (e) {
                e.preventDefault()
                var isValid = true;
                var id = $('#id').val();
                var name = $('#name').val();

                // 商品类别ID验证
                if (id === '') {
                    $('#idError').show();
                    isValid = false;
                } else if (!/^\d+$/.test(id)) {
                    $('#idError').text("商品类别ID必须为正整数").show();
                    isValid = false;
                } else {
                    $('#idError').hide();
                }

                // 商品类别名称验证
                if (name === '') {
                    $('#nameError').show();
                    isValid = false;
                } else {
                    $('#nameError').hide();
                }

                if (!isValid) {
                    e.preventDefault();
                }else {
                    var formData = $(this).serialize();
                    $.ajax({
                        url: 'category.action?action=save',
                        type: 'POST',
                        data: formData,
                        success: function (result) {
                            if (result.startsWith("添加成功")) {
                                Swal.fire({
                                    icon: 'success',
                                    title: '成功',
                                    text: '商品种类添加成功',
                                }).then(function () {
                                    // 刷新到指定页面，例如 index.html
                                    window.location.href = 'supplier.action?action=list';
                                });
                            } else {
                                Swal.fire({
                                    icon: 'error',
                                    title: '失败',
                                    text: result
                                });
                            }
                        },
                        error: function (xhr, status, error) {
                            Swal.fire({
                                icon: 'error',
                                title: '请求出错',
                                text: '出现网络问题或服务器错误，请稍后再试'
                            });
                        }
                    });
                }
            });
        });
    </script>
</div>